package com.example.appointment.fragment;

import android.support.v4.app.Fragment;
import android.view.View;

import com.example.appointment.R;
import com.example.appointment.meta.EventBusVO;
import com.example.appointment.meta.MyCollectionSetVO;
import com.example.appointment.util.Constant;

import butterknife.OnClick;
import de.greenrobot.event.EventBus;

/**
 * 我的收藏界面
 * Created by Admin on 2016/8/31.
 */
public class MyCollectionFragment extends BaseFragment {
    private boolean isselectall = false;//判断是否是选中全部的flag

    @Override
    protected void initView() {
        inittitle();
        createFragment(MyCollectionSetFragment.class.getName());


    }

    /**
     * 创建碎片对象
     *
     * @param fragmentName
     */
    private void createFragment(String fragmentName) {
        Fragment fragment = Fragment.instantiate(getActivity(), fragmentName);
        getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.fragment_mycollection_fragment, fragment)
                .commit();
    }

    private void inittitle() {
        doublelayout.setVisibility(View.VISIBLE);
        titile.setVisibility(View.GONE);
        backbtn.setVisibility(View.GONE);
    }

    @Override
    protected int getLayoutId() {

        return R.layout.fragment_mycollection;
    }

    /**
     * 选贼系列
     */
    @OnClick(R.id.leaft_title)
    public void leftpre(View view) {
        super.leftpre(view);
        //替换为系列
        if (backbtn.getVisibility() == View.VISIBLE) {
            backbtn.setVisibility(View.GONE);
        }
        if (selectall.getVisibility() == View.VISIBLE) {
            selectall.setVisibility(View.GONE);
            selectall.setImageResource(R.mipmap.choose);
        }

        createFragment(MyCollectionSetFragment.class.getName());

    }

    /**
     * 选择视频
     */
    @OnClick(R.id.right_title)
    public void rightpre(View view) {
        super.rightpre(view);
        //替换为视频

        if (backbtn.getVisibility() == View.VISIBLE) {
            backbtn.setVisibility(View.GONE);
        }
        if (selectall.getVisibility() == View.VISIBLE) {
            selectall.setVisibility(View.GONE);
            selectall.setImageResource(R.mipmap.choose);
        }
        createFragment(MyCollectionVideoFragment.class.getName());
    }

    /**
     * 与发布者在同一个线程
     */
    public void onEvent(MyCollectionSetVO itemvo) {
        backbtn.setVisibility(View.VISIBLE);
        selectall.setVisibility(View.VISIBLE);
        backbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(backbtn);
                backbtn.setVisibility(View.GONE);
                selectall.setVisibility(View.GONE);
                selectall.setImageResource(R.mipmap.choose);

            }
        });

        selectall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBusVO busVO = new EventBusVO();
                if (isselectall) {
                    //这里全不选
                    selectall.setImageResource(R.mipmap.choose);
                    busVO.what = Constant.UNSELECTALL;
                    EventBus.getDefault().post(busVO);
                    isselectall = false;
                } else {
                    //这里全选
                    selectall.setImageResource(R.mipmap.choose_all);
                    busVO.what = Constant.SELECTALL;
                    EventBus.getDefault().post(busVO);
                    isselectall = true;
                }
            }
        });
    }

}
